package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.document.DocumentService;
import com.qdxwx.models.document.Document;
import com.qdxwx.models.document.DocumentQuery;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/ajax/document")
public class AjaxDocumentController  extends BaseController {

    @Autowired
    private DocumentService documentService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/get.json")
    @ResponseBody
    public AjaxResult view(HttpServletRequest request){
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return AjaxResult.error("参数异常");
        Document document = documentService.get(id);
        if(document == null) return AjaxResult.error("找不到指定素材");
        return AjaxResult.success(document);
    }

    @GetMapping("/list.json")
    @ResponseBody
    public AjaxResult list(HttpServletRequest request){
        Integer channelId = TKConvert.toInteger(request.getParameter("channelId"),11);
        Integer categoryId = TKConvert.toInteger(request.getParameter("categoryId"),null);
        Integer elite = TKConvert.toInteger(request.getParameter("elite"),null);
        Integer isTop = TKConvert.toInteger(request.getParameter("isTop"),null);
        Integer levelId = TKConvert.toInteger(request.getParameter("levelId"),null);
        Integer isFree = TKConvert.toInteger(request.getParameter("isFree"),null);
        Integer orderBy = TKConvert.toInteger(request.getParameter("orderBy"));
        Long userId = TKConvert.toLong(request.getParameter("userId"));
        Integer page = TKConvert.toInteger(request.getParameter("page"),1);
        int limit = TKConvert.toInteger(request.getParameter("limit"),10);
        DocumentQuery query = new DocumentQuery();
        query.setChannelId(channelId);
        query.setIsTop(isTop);
        query.setIsElite(elite);
        query.setCategoryId(categoryId);
        query.setOrderBy(orderBy);
        query.setStatus(1);
        query.setUserId(userId);
        query.setLevelId(levelId);
        query.setIsFree(isFree);
        if (categoryId > 0) {
            query.setCategoryIds(categoryService.getChildIds(categoryId));
        }
        PageRequest pageRequest = new PageRequest();
        pageRequest.setCurrentPage(page);
        pageRequest.setPageSize(limit);
        List<Document> documentList = documentService.getList(query, pageRequest);
        return AjaxResult.success(documentList);
    }
}
